package application;

import java.util.HashMap;
import java.util.Map;

/**
 * Implements the functionality of the second NAUI table aka the Surface
 * Interval Time (SIT) Table. This table is only accessed when user selects for
 * multiple dives.
 * 
 * Multiple Dives:
 * 
 * <> Input: (fr: Table 1) char letterGroup (fr: User) int SIT - assumes the
 * letterGroup is valid and the SIT is possible
 * 
 * <> Output: char letterGroup - used by Table 3
 * 
 * Other Assumptions:
 *
 * <> Table 2 assumes that GUI will prevent impossible inputs
 *
 */
public class SecondNAUITable implements NAUITable {
	private int SIT;
	private char letterGroup;
	private Map<Character, Integer[]> table;

	/**
	 * Creates Table 2 and sets initial values.
	 */
	public SecondNAUITable() {
		this.letterGroup = DivePlan.INITIAL_GROUP;
		this.SIT = 0;

		table = new HashMap<Character, Integer[]>();
		this.createTable();
	}

	/**
	 * Changes the current letterGroup input for the table. Calculates and sets
	 * new letterGroup based on change.
	 * 
	 * @param letterGroup
	 *            value from Table 1
	 */
	public void setGroup(char letterGroup) {
		this.setGroupAndSIT(letterGroup, this.SIT);
	}

	/**
	 * Changes the current SIT input for the table. Calculates and sets new
	 * letterGroup based on change.
	 * 
	 * @param SIT
	 *            user's desired surface time interval
	 */
	public void setSIT(int SIT) {
		this.setGroupAndSIT(this.letterGroup, SIT);
	}

	/**
	 * Changes the current letterGroup and SIT input for the table. Calculates
	 * and sets new letterGroup based on change.
	 * 
	 * @param letterGroup
	 *            value from Table 1
	 * @param SIT
	 *            user's desired surface time interval
	 */
	public void setGroupAndSIT(char letterGroup, int SIT) {
		this.letterGroup = letterGroup;
		this.SIT = SIT;

		this.calcOutput();
	}

	/**
	 * Calculates and sets letterGroup based on current values for letterGroup
	 * and SIT inputs.
	 */
	private void calcOutput() {
		Integer[] value = table.get(this.letterGroup);
		for (int i = 0; i < value.length; i++) {
			if (this.SIT >= value[i]) {
				this.letterGroup = (char) (i + 'A');
				break;
			}
		}
	}

	/**
	 * Retrieves currently set letterGroup.
	 * 
	 * If letterGroup is '?', the inputs for letterGroup and/or SIT have NEVER
	 * been given.
	 * 
	 * @return letterGroup calculated
	 */
	public char getLetterGroup() {
		return letterGroup;
	}

	/**
	 * Creates Table 2.
	 */
	private void createTable() {
		Integer[] A = { 10 };
		Integer[] B = { 201, 10 };
		Integer[] C = { 290, 100, 10 };
		Integer[] D = { 349, 159, 70, 10 };
		Integer[] E = { 395, 205, 118, 55, 10 };
		Integer[] F = { 426, 238, 149, 90, 46, 10 };
		Integer[] G = { 456, 266, 179, 120, 76, 41, 10 };
		Integer[] H = { 480, 290, 201, 154, 102, 67, 37, 10 };
		Integer[] I = { 502, 313, 224, 165, 123, 90, 60, 34, 10 };
		Integer[] J = { 531, 341, 243, 185, 141, 108, 80, 55, 32, 10 };
		Integer[] K = { 539, 349, 260, 202, 159, 124, 96, 72, 50, 29, 10 };
		Integer[] L = { 553, 363, 276, 217, 174, 140, 110, 86, 65, 46, 27, 10 };
		table.put('A', A);
		table.put('B', B);
		table.put('C', C);
		table.put('D', D);
		table.put('E', E);
		table.put('F', F);
		table.put('G', G);
		table.put('H', H);
		table.put('I', I);
		table.put('J', J);
		table.put('K', K);
		table.put('L', L);
	}

	public Map<Character, Integer[]> getTable() {
		return this.table;
	}
}
